package cn.redsoft.turingapi.plugin;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.redsoft.turingapi.exception.WechatException;
import cn.redsoft.turingapi.msg.BaseMsg;
import cn.redsoft.turingapi.msg.CookBookMsg;
import cn.redsoft.turingapi.msg.CookBookMsg.CookBookItem;
import cn.redsoft.turingapi.msg.LinkMsg;
import cn.redsoft.turingapi.msg.NewsMsg;
import cn.redsoft.turingapi.msg.NewsMsg.NewsItem;
import cn.redsoft.turingapi.msg.TextMsg;
import cn.redsoft.wechatapi.msg.resp.passivity.NewsResponseMsg;
import cn.redsoft.wechatapi.msg.resp.passivity.NewsResponseMsg.Article;
import cn.redsoft.wechatapi.msg.resp.passivity.PassivityResponseMsg;
import cn.redsoft.wechatapi.msg.resp.passivity.TextResponseMsg;

public class WechatMsgConverter {

	public PassivityResponseMsg converte(BaseMsg baseMsg) {

		if (baseMsg instanceof TextMsg) {
			return this.converteFromTextMsg((TextMsg) baseMsg);
		} else if (baseMsg instanceof NewsMsg) {
			return this.converteFromNewsMsg((NewsMsg) baseMsg);
		} else if (baseMsg instanceof CookBookMsg) {
			return this.converteFromCookBookMsg((CookBookMsg) baseMsg);
		} else if (baseMsg instanceof LinkMsg) {
			return this.converteFromLinkMsg((LinkMsg) baseMsg);
		}
		return null;
	}

	protected PassivityResponseMsg converteFromLinkMsg(LinkMsg link) {
		TextResponseMsg text = new TextResponseMsg();
		text.setContent("<a href=\"" + link.getUrl() + "\">" + link.getText()
				+ "</a>");
		return text;
	}

	protected PassivityResponseMsg converteFromCookBookMsg(CookBookMsg cook) {
		NewsResponseMsg resp = new NewsResponseMsg();
		List<CookBookItem> cookList = cook.getList();
		if (cookList != null && cookList.size() >= 0) {
			int size = cookList.size() <= 9 ? cookList.size() : 9;
			resp.setArticleCount(size);
			resp.setCreateTime(new Date().getTime());
			List<Article> articles = new ArrayList<NewsResponseMsg.Article>();
			for (int i = 0; i < size; i++) {
				CookBookItem item = cookList.get(i);
				Article article = new Article();
				article.setTitle(item.getName());
				article.setUrl(item.getDetailurl());
				article.setPicUrl(item.getIcon());
				article.setDescription(item.getInfo());
				articles.add(article);
			}
			resp.setArticles(articles);
		} else {
			throw new WechatException("返回菜谱列表为空.");
		}
		return resp;
	}

	protected PassivityResponseMsg converteFromNewsMsg(NewsMsg news) {

		NewsResponseMsg resp = new NewsResponseMsg();
		List<NewsItem> turingNewsList = news.getList();
		if (turingNewsList != null && turingNewsList.size() >= 0) {
			int size = turingNewsList.size() <= 9 ? turingNewsList.size() : 9;
			resp.setArticleCount(size);
			resp.setCreateTime(new Date().getTime());

			List<Article> articles = new ArrayList<NewsResponseMsg.Article>();
			for (int i = 0; i < size; i++) {
				NewsItem item = turingNewsList.get(i);
				Article article = new Article();
				this.fillArticleByTuringNewsItem(item, article);
				articles.add(article);
			}
			resp.setArticles(articles);
		} else {
			throw new WechatException("返回新闻消息列表为空.");
		}
		return resp;
	}

	private void fillArticleByTuringNewsItem(NewsItem item, Article article) {
		article.setDescription(item.getSource());
		article.setPicUrl(item.getIcon());
		if (item.getSource() != null && !"".equals(item.getSource()))
			article.setTitle(item.getArticle() + " (" + item.getSource() + ")");
		else
			article.setTitle(item.getArticle());
		article.setUrl(item.getDetailurl());
	}

	protected TextResponseMsg converteFromTextMsg(TextMsg text) {
		TextResponseMsg msg = new TextResponseMsg();
		msg.setContent(text.getText());
		msg.setCreateTime(new Date().getTime());
		return msg;
	}

}
